<?php


namespace core\lib;


class Cache {

    protected static $redis;

    protected static function getInstance() {
        if (is_null(self::$redis)) {
            self::$redis = new \Redis();
            self::$redis->pconnect(config('redis.cache.host'), config('redis.cache.port'));
            if (!empty(config('redis.cache.password'))) {
                self::$redis->auth(config('redis.cache.password'));
            }
            self::$redis->select(config('redis.cache.db'));
        }
        return self::$redis;
    }

    public static function set(string $key, $value, $timeout = 3600) {
        return self::getInstance()->set($key, $value, $timeout);
    }

    public static function get(string $key) {
        return self::getInstance()->get($key);
    }

    public static function sAdd(string $key, $value) {
        $value = (array)$value;
        return self::getInstance()->sAdd($key, ...$value);
    }

    public static function sMembers(string $key) {
        return self::getInstance()->sMembers($key);
    }

    public static function delete(string $key) {
        return self::getInstance()->del($key);
    }

}
